<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Course Service class for communication between controllers and persistent storage
 */
class Core_Service_Course extends RedCross_Service_ServiceAbstract
{

  /**
   * Constant to return if one tries to archive a course that other
   * roles points to. The member should then be told to delete these
   * relations before being allowed to archive the course.
   * @var string
   */
  const ARCHIVE_FAIL_ROLES_RELATIONS = 'ARCHIVE_FAIL_ROLES_RELATIONS';

    /**
     * Constructor
     * @param Core_Gateway_User $gateway
     */
    public function __construct(Core_Gateway_Course $gateway)
    {
        parent::__construct($gateway, 'Core_Model_Course');
    }

    public function fetchAll($paginate = false)
    {
      $this->checkAcl('fetch');
      return $this->_gateway->fetchAll($paginate);
    }

    public function fetchAllCourses($paginate = false)
    {
        $this->checkAcl('fetch');
        return $this->_gateway->fetchAllCourses($paginate);
    }

    public function fetchAllCompetences($paginate = false)
    {
        $this->checkAcl('fetch');
        return $this->_gateway->fetchAllCompetences($paginate);
    }

    public function fetchAllNonArchived($paginate = false)
    {
        $this->checkAcl('fetch');
      return $this->_gateway->fetchAllNonArchived($paginate);
    }

    public function fetchAllArchived($paginate = false)
    {
        $this->checkAcl('fetch');
      return $this->_gateway->fetchAllArchived($paginate);
    }

    public function create(array $data)
    {
        $this->checkAcl('create');

        $name = $data['name'];
        $months_valid = (int) $data['months_valid'];
        $type = $data['type'];

        $course = new Core_Model_Course();
        $course->name = $name;
        $course->type = $type;

        if ($months_valid > 0){
            $course->months_valid = $months_valid;
        }
        return $this->save($course);
    }

    public function edit($course_id, array $data)
    {
        $this->checkAcl('edit');

        $course = $this->getGateway()->fetch((int) $course_id);
        if (!$course) {
          return false;
        }

        $course->name = $data['name'];
        $months_valid = (int) $data['months_valid'];
        $course->type = $data['type'];

       if ($months_valid > 0) {
           $course->months_valid = $months_valid;
       } else {
           $course->months_valid = null;
       }
       return $this->save($course) != false;
    }

    public function toggleArchive($course_id, array $data)
    {
        $this->checkAcl('edit');

        $course = $this->getGateway()->fetch((int) $course_id);
        if (!$course)
        {
          return false;
        }

        if (is_null($course->archived_date))
        {
          $numbersOfRolesRequiringThisCourse = count($course->RoleCourse);
          if ($numbersOfRolesRequiringThisCourse > 0)
          {
            return self::ARCHIVE_FAIL_ROLES_RELATIONS;
          }

          $date = new RedCross_Date();
          $course->archived_date = $date->export();
        } else
        {
          $course->archived_date = null;
        }
        return $this->save($course) != false;
     }

    public function getValidator()
    {

    }

    public function fetchAllForMember($memberId)
    {
        return $this->getGateway()->fetchAllForMember($memberId);
    }

    public function fetchAllCoursesForMember($memberId)
    {
        return $this->getGateway()->fetchAllCoursesForMember($memberId);
    }

    public function fetchAllCompetencesForMember($memberId)
    {
        return $this->getGateway()->fetchAllCompetencesForMember($memberId);
    }

    public function fetchAllExpiringCoursesForMember($memberId) {
        return $this->getGateway()->fetchAllExpiringCoursesForMember($memberId);
    }
}
